<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <title>登陆 - Blog</title>
    <link rel="stylesheet" href="./static/layui/css/layui.css">
    <link rel="stylesheet" href="./static/css/common.css">
    <link rel="stylesheet" href="./static/css/login.css">
    <script type="text/javascript" src="static/js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">


    </script>

</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md12 layui-hide-xs xs-hide"></div>
        <div class="layui-col-md4 layui-col-md-offset4 login-main">
            <img src="./static/images/logo.png" class="logo">
            <div id="login_div" class="main formbox">
                <form action="login_submit" method="get" accept-charset="utf-8" class="layui-form">
                    <div class="inputbox">
                        <input type="text" name="title" required lay-verify="required" placeholder="请输入邮箱账号"
                               autocomplete="off" class="layui-input">
                    </div>

                    <div class="margin20"></div>
                    <div class="inputbox">
                        <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="margin20"></div>
                    <div class="inputbox info">
                        <input type="checkbox" name="checkbox" lay-skin="primary" checked/>
                        我已阅读并同意<a href="javascript:;">用户协议</a>和<a href="javascript:;">隐私条款</a>
                    </div>

                    <div class="layui-row layui-col-space20 bts">
                        <div class="layui-col-md6">
                            <button id="to_main_btn" type="button" class="layui-btn" lay-submit lay-filter="formDemo">登陆</button>
                        </div>
                        <div class="layui-col-md6">
                            <a href="javascript:clickReg()" class="layui-btn layui-btn-normal">去注册</a>
                        </div>
                        <div class="layui-col-md12">
                            <a href="index.html" class="layui-btn layui-btn-primary">返回首页</a>
                        </div>
                    </div>
                </form>
            </div>
            <div id="register_div" class="main formbox" hidden>
                <form action="login_submit" method="get" accept-charset="utf-8" class="layui-form">
                    <div class="inputbox">
                        <input id="dd1" type="text" name="title" required lay-verify="required" placeholder="请输入邮箱账号"
                               autocomplete="off" class="layui-input">
                    </div>

                    <div class="margin20"></div>
                    <div class="inputbox">
                        <input id="dd2" type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>

                    <div class="margin20"></div>
                    <div class="inputbox">
                        <input id="dd3" type="password" name="password" required lay-verify="required" placeholder="请确认密码"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="inputbox info">
                        <input type="checkbox" name="checkbox" lay-skin="primary" checked/>
                        我已阅读并同意<a href="javascript:;">用户协议</a>和<a href="javascript:;">隐私条款</a>
                    </div>

                    <div class="layui-row layui-col-space20 bts">
                        <div class="layui-col-md6">
                            <a href="javascript:clickLog()"id="button2" class="layui-btn layui-btn-normal">去登陆</a>
                        </div>
                        <div class="layui-col-md6">
                            <input id="button1" type="button" class="layui-btn" value="确认注册" lay-submit lay-filter="formDemo">
                        </div>
                        <div class="layui-col-md12">
                            <a href="index.html" class="layui-btn layui-btn-primary">返回首页</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

</body>
<script src="./static/layui/layui.all.js"></script>
<script src="./static/js/jquery-1.12.4.min.js"></script>
<script>
    function clickReg() {
        $("#register_div").show();
        $("#login_div").hide();
    }
    function clickLog() {
        $("#login_div").show();
        $("#register_div").hide();
    }
    $(document).ready(function () {
        $("#button1").click(function () {
        var username =$("[id='dd1']").val();
        var password=$("[id='dd2']").val();
        var password2=$("[id='dd3']").val();
        if (password == password2){
            $.get("User/add",{"username":username,"password":password},function (rs) {
                var result = rs.result;
                if (result!="") {
                    alert("注册成功")
                } else {
                    alert("注册失败")
                }
            });
        }else {
            alert("两次密码不同");
        }
    });
        $("#to_main_btn").click(function () {
            var username = $("[name='title']").val();
            var password = $("[name='password']").val();
            $.get("User/login", {"username": username, "password": password}, function (user) {
                if (user.username == username && user.password == password) {
                    if (user.identity==1) {
                        alert("登陆成功");
                        window.location = `main.html?identity=1&uid=${user.uid}&username=${user.username}`;
                        sessionStorage.setItem('uid',user.uid);
                        sessionStorage.setItem('username',user.username);
                        sessionStorage.setItem('identity',user.identity);
                    } else {
                        alert("登陆成功");
                        window.location = `main.html?identity=0&uid=${user.uid}&username=${user.username}`;
                        sessionStorage.setItem('uid',user.uid);
                        sessionStorage.setItem('username',user.username);//port: 8082
                    }
                } else {
                    alert("登录失败");
                    window.location = 'login.html';
                }
            });
        });
    });
</script>
</html>